package ar.com.bdf.ipharma.stress;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.comm.SerialPort;

import ar.com.bdf.ipharma.communications.BufferBox;
import ar.com.bdf.ipharma.communications.RunnableLine;
import ar.com.bdf.ipharma.communications.serial.SimpleRead;
import ar.com.bdf.ipharma.protocolo.model.ParserTrama;
import ar.com.bdf.ipharma.stress.operation.OperationAbrirPuerta;
import ar.com.bdf.ipharma.stress.operation.OperationCerrarPuerta;
import ar.com.bdf.ipharma.stress.operation.OperationExtraer;
import ar.com.bdf.ipharma.protocolo.model.Trama;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;



public class MainGui {

	
	
	private SimpleRead reader = null;
	private SerialPort  serialPort=null;
	
	
	
	public MainGui() {
		initThreadReadPort();
	}
	
	public static void main(String[] args) throws IOException {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MainGui();
			}
		});
		
		
		
    	
		Task task = new Task();
		List<Operation> operations = new ArrayList<Operation>();
		OperationExtraer extraer = new OperationExtraer(1,1,1);
		extraer.setCountOperation(new Long(1));
		operations.add(extraer);
		
		/*
		OperationAbrirPuerta abrirPuerta = new OperationAbrirPuerta();
		abrirPuerta.setCountOperation(new Long(1));
		operations.add(abrirPuerta);
		OperationCerrarPuerta cerrarPuerta = new OperationCerrarPuerta();
		cerrarPuerta.setCountOperation(new Long(1));
		operations.add(cerrarPuerta);
		*/
		task.setIdentificador(5);
		task.setOperations(operations);
		task.setPort("COM5");
		task.setTimeInterOperation(new Long(1));
		
		
		
		System.out.println("Se empezar a ejecutar el test");
		task.execute();
		System.out.println("Finalizo el test");
		
	}

	
	public void initThreadReadPort() {
		RunnableLine runnerLine = new RunnableCom();
		this.reader = new SimpleRead(runnerLine, getPort());
	}
	
	
	private String getPort() {
		return "COM5"; //ropertiesManager.get("port");
	}
	
	private static XStream getXstreamConfigure() {
		XStream xStream = new XStream(new DomDriver());
		
		xStream.alias(Task.class.getSimpleName(), Task.class);
		xStream.omitField(Task.class, "parser");
		xStream.useAttributeFor(Task.class, "port");
		xStream.useAttributeFor(Task.class, "timeInterOperation");
		xStream.useAttributeFor(Task.class, "identificador");
		
		xStream.useAttributeFor(Operation.class, "countOperation");
		xStream.omitField(Operation.class, "handShake");
		xStream.omitField(Operation.class, "factory");
		
		xStream.alias(OperationExtraer.class.getSimpleName(), OperationExtraer.class);
		xStream.useAttributeFor(OperationExtraer.class, "cantVueltas");
		xStream.useAttributeFor(OperationExtraer.class, "posicionRulo");
		xStream.useAttributeFor(OperationExtraer.class, "nivel");

		xStream.alias(OperationAbrirPuerta.class.getSimpleName(), OperationAbrirPuerta.class);
		
		xStream.alias(OperationCerrarPuerta.class.getSimpleName(), OperationCerrarPuerta.class);

		return xStream;
	}
	
	
	private class RunnableCom implements RunnableLine {
		private BufferBox buffer = new BufferBox(new ShowTextAreaBuffer());

		public void execute(String line) {
			buffer.addInBuffer(line);
		}

		
	}

	private class ShowTextAreaBuffer implements RunnableLine {
		
		public void execute(String response) {
			System.out.println(getPort()+ "<=== " + response);
			Trama trama = new ParserTrama().unserializable(response);
			System.out.println(trama.getDatos());
		}

		
	}
}
